package com.a2.appa3.ui.activity;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.update_PwdBean;
import com.a2.appa3.util.Md5;
import com.a2.appa3.util.SYSOUtils;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.shihao.library.XStatusBarHelper;

public class PasswordActivity extends BaseActivitySimple {

    @BindView(R.id.ivback)
    ImageView ivback;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.headerButton)
    ImageView headerButton;
    @BindView(R.id.headerButtonRoot)
    LinearLayout headerButtonRoot;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.mima1)
    EditText mima1;
    @BindView(R.id.mima2)
    EditText mima2;
    @BindView(R.id.mima3)
    EditText mima3;
    @BindView(R.id.conform)
    Button conform;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        headerTitle.setText("修改密码");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mima1.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivity.this, "请输入登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mima2.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivity.this, "请输入新登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mima3.getText().toString().isEmpty()) {
                    Toast.makeText(PasswordActivity.this, "请输入新登录密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mima2.getText().toString().equals(mima3.getText().toString())) {
                    Toast.makeText(PasswordActivity.this, "两次输入的密码请保持一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!mima2.getText().toString().trim().matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z_]{6,}$")) {
                    Toast.makeText(PasswordActivity.this, "您的密码不符合规定", Toast.LENGTH_SHORT).show();
                    return;
                }
                conform();
            }
        });

    }

    private void conform() {
        HttpJsonUtils.getHttpApiInstance()
                .get_updat_loginpwd(HttpAdress.getuseAdress() + "api/m/user/updateMyPwd", Md5.toMd5(mima1.getText().toString()),
                        Md5.toMd5(mima2.getText().toString()), Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext(update_PwdBean resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                Toast.makeText(PasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(PasswordActivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }

    @Override
    protected int setContentView() {
        return R.layout.activity_password;
    }
}
